using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_CommonEquip : UIBaseNode
    {
        #region Template Generate,don't modify
        protected partial class UIB_UIN_CommonEquip
        {
            #region ObjectBinding Generate
            public UnityEngine.GameObject empty { protected set; get; }
            public IQIGame.Onigao.Framework.ExRawImage iconEquip { protected set; get; }
            public IQIGame.Onigao.Framework.ExArtSpriteNum qualityFrame { protected set; get; }
            public IQIGame.Onigao.Framework.ExArtSpriteNum qualityImage { protected set; get; }
            public UnityEngine.GameObject star { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textLv { protected set; get; }
            public UnityEngine.GameObject type { protected set; get; }
            public IQIGame.Onigao.Framework.ExArtSpriteNum iconType { protected set; get; }
            public UnityEngine.GameObject wear { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage iconHeroHead { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnView { protected set; get; }
            public UnityEngine.GameObject locked { protected set; get; }
            public UnityEngine.GameObject lockSelected { protected set; get; }
            public UnityEngine.GameObject selected { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("empty", out var __tbv0);
                this.empty = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExRawImage>("iconEquip", out var __tbv1);
                this.iconEquip = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExArtSpriteNum>("qualityFrame", out var __tbv2);
                this.qualityFrame = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExArtSpriteNum>("qualityImage", out var __tbv3);
                this.qualityImage = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("star", out var __tbv4);
                this.star = __tbv4;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textLv", out var __tbv5);
                this.textLv = __tbv5;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("type", out var __tbv6);
                this.type = __tbv6;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExArtSpriteNum>("iconType", out var __tbv7);
                this.iconType = __tbv7;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("wear", out var __tbv8);
                this.wear = __tbv8;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("iconHeroHead", out var __tbv9);
                this.iconHeroHead = __tbv9;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnView", out var __tbv10);
                this.btnView = __tbv10;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("locked", out var __tbv11);
                this.locked = __tbv11;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("lockSelected", out var __tbv12);
                this.lockSelected = __tbv12;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("selected", out var __tbv13);
                this.selected = __tbv13;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        ListScrollAdapter<UIN_CommonDarkLight> stars = null;

        bool isInitClickSelf = false;
        int dataIndex = -1;
        bool isTemplateEquipItem = false;
        ItemEquipmentData equipItem = null;
        CfgItem cfgEquipItem = null;
        Action<int, UIN_CommonEquip> callOnClickSelf = null;
        Action<int, UIN_CommonEquip> callOnClickView = null;
        bool isViewOneLvOneStar = false;

        protected UIB_UIN_CommonEquip ui { get; set; }
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_CommonEquip();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.stars = this.InitListScroll<UIN_CommonDarkLight>(this.ui.star, this.OnStarChanged);
            this.ui.btnView.onClick.AddListener(this.OnClickView);

            this.SetViewActive(false);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {
            this.ClearData();
        }

        protected override void OnDispose()
        {
            this.callOnClickSelf = null;
            this.callOnClickView = null;
            this.ClearData();
        }

        void OnClickSelf()
        {
            this.callOnClickSelf?.Invoke(this.dataIndex, this);
        }

        void OnClickView()
        {
            this.callOnClickView?.Invoke(this.dataIndex, this);
        }

        public void SetCallClickSelf(Action<int, UIN_CommonEquip> onClick)
        {
            this.InitClickSelf();
            this.callOnClickSelf = onClick;
        }

        private void InitClickSelf()
        {
            if (this.isInitClickSelf)
                return;
            this.isInitClickSelf = true;
            var btnSelf = this.gameObject.GetOrAdd<ExButton>();
            btnSelf.onClick.AddListener(this.OnClickSelf);
        }

        public void SetCallClickView(Action<int, UIN_CommonEquip> onClick)
        {
            this.callOnClickView = onClick;
        }

        public void SetViewActive(bool active)
        {
            this.ui.btnView.SetActive(active);
        }

        public bool ShowTemplate(int itemCid, int itemNum)
        {
            ItemEquipmentData data = ItemEquipmentData.ConvertFromCidNum(itemCid, itemNum);
            this.Show(data);
            bool isShowing = (this.equipItem != null);
            if (isShowing)
                this.isTemplateEquipItem = true;
            return isShowing;
        }

        public void ShowOneLvOneStar(ItemEquipmentData data)
        {
            this.Show(data);
            if (this.equipItem != null)
            {
                this.isViewOneLvOneStar = true;
                this.ViewLv(1);
                this.ViewStar();
            }
        }

        public void Show(ItemEquipmentData data, int index = -1)
        {
            if (data == null)
            {
                this.Hide();
                return;
            }
            this.isTemplateEquipItem = false;
            this.isViewOneLvOneStar = false;
            bool isChanged = this.equipItem == null || this.equipItem != data;
            this.dataIndex = index;
            this.equipItem = data;
            this.cfgEquipItem = data.cfgData;

            this.Show();
            this.SetLockSelected(false);
            this.SetSelected(false);

            if (isChanged)
            {
                this.SetEmptyActive(false);
                this.ViewQualityAndIconType();
            }

            this.ViewSkin();
            this.ViewLv(equipItem.level);
            this.ViewStar();
            this.ViewWear();
            this.SetLocked(equipItem.isLock);
        }

        private void ClearData()
        {
            this.dataIndex = -1;
            if (this.isTemplateEquipItem)
            {
                this.equipItem?.Put2Pool();
                this.isTemplateEquipItem = false;
            }
            this.equipItem = null;
            this.isViewOneLvOneStar = false;
        }

        void ViewQualityAndIconType()
        {
            CfgItem cfgItem = this.cfgEquipItem;
            this.ui.qualityFrame.SetInt(cfgItem.Quality);
            this.ui.qualityImage.SetInt(cfgItem.Quality);
            this.ui.iconType.SetInt(cfgItem.SubType);
        }

        void ViewSkin()
        {
            CfgEquipSkin cfgEquipSkin = equipItem.cfgSkin;
            CfgItem cfgItem = this.cfgEquipItem;
            string cardPath = cfgEquipSkin?.Card_fullPath ?? cfgItem.Card_fullPath;
            this.SetRawImageTexture(this.ui.iconEquip, cardPath);
        }

        void ViewLv(int lv)
        {
            this.ui.textLv.text = lv.ToString();
        }

        void ViewStar()
        {
            this.stars.RefillCells(equipItem.maxStar);
        }

        void ViewWear()
        {
            HeroData wearer = this.equipItem.currWearer;
            bool isWearing = wearer != null;
            this.ui.wear.SetActive(isWearing);
            if (!isWearing)
                return;
            this.SetImageSprite(this.ui.iconHeroHead, wearer.cfgSkin.HeroHeadImage_fullPath);
        }

        protected void SetEmptyActive(bool isActive)
        {
            this.ui.empty.SetActive(isActive);
        }

        public void SetSelected(bool isSelected)
        {
            this.ui.selected.SetActive(isSelected);
        }

        protected void SetLocked(bool locked)
        {
            this.ui.locked.SetActive(locked);
        }

        public void SetLockSelected(bool isLockSelected)
        {
            this.ui.lockSelected.SetActive(isLockSelected);
        }

        public void SetLockState(bool locked, bool isLockSelected = false)
        {
            this.SetLocked(locked);
            this.SetLockSelected(isLockSelected);
        }

        void OnStarChanged(UIN_CommonDarkLight item, int index)
        {
            int star = this.isViewOneLvOneStar ? 1 : this.equipItem.star;
            bool isLight = star >= (index + 1);
            item.Show(isLight);
        }
    }
}
